//
//  XQOrderListPageController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQOrderListPageController.h"
#import "XQOrderListPageCell.h"
#import "XQOrderEmptyView.h"

@interface XQOrderListPageController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger moreDataFlag;
@property (nonatomic,strong) XQOrderEmptyView *emptyView;
@end

@implementation XQOrderListPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makeUI];
    [self makeConstraints];
    
    self.moreDataFlag = 1;
    
    [XQHUD showLoadingInView:self.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self refreshData];
}

- (void)makeUI {
    self.view.backgroundColor = kCardColor;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.emptyView];
    [self updatePageStatus:1];
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.center;
    }];
}

#pragma mark - private

- (void)refreshData {
    
    NSDictionary *dic = @{
        @"orient" :  self.orient
    };
    
    @weakify(self)
    [XQAFNetworking postWithUrl:[XQNetManager getAbsolutePath:@"/direct/objective"] pars:dic callback:^(_Bool success, id  _Nonnull responseObject, NSError * _Nonnull error) {
        @strongify(self);
        [XQHUD hideLoadindInView:self.view];
        [self updatePageStatus:0];
        if ([responseObject[@"concept"] integerValue] == 0) {
            NSDictionary *accomplish = responseObject[@"accomplish"];
            id rakuten = accomplish[@"rakuten"];
            if ([rakuten isKindOfClass:[NSArray class]]) {
                NSArray *rakutenArr = rakuten;
                if (rakutenArr.count) {
                    [self updatePageStatus:1];
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:rakuten];
                }
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
    }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    
    XQOrderListPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQOrderListPageCell" forIndexPath:indexPath];
    [cell setImageName:@"order_card_blue"];
    [cell setLogoImageName:dic[@"networker"]];
    [cell setLogoTitle:dic[@"airborne"]];
    [cell setTitle:dic[@"hughes"]];
    [cell setPrice:dic[@"policies"]];
    [cell setEffects:dic[@"effects"]];
    [cell setPrivacy:dic[@"transmitting"]];
    @weakify(self);
    cell.agreeHandle = ^{
        @strongify(self);
        [self privacyClick:dic[@"pinned"] indexPath:indexPath];
    };
    cell.loanHandle = ^{
        @strongify(self);
        [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    
    NSString *url = dic[@"advancement"];
    
    if (url.length) {
        [XQRouter jumpFromController:self url:url];
    }
}

#pragma mark - setter

- (void)setOrient:(NSString *)orient {
    _orient = orient;
    [self refreshData];
}

#pragma mark - private

// index == 0 empty 1 success
- (void)updatePageStatus:(NSInteger)index {
    self.emptyView.hidden = index != 0;
    self.tableView.hidden = index != 1;
}

- (void)privacyClick:(NSString *)pricacy indexPath:(NSIndexPath *)indexPath {
    if (pricacy.length) {
        [XQRouter jumpFromController:self url:[XQNetManager getWebUrlAbsolutePath:pricacy]];
    }else {
        NSDictionary *dic = self.dataSource[indexPath.row];
        
        XQWebViewController *controller = [[XQWebViewController alloc] init];
        
        [controller setUrl:[XQNetManager getWebUrlAbsolutePath:dic[@"advancement"]]];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[XQOrderListPageCell class] forCellReuseIdentifier:@"XQOrderListPageCell"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshData];
        }];
    }
    return _tableView;
}

- (XQOrderEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[XQOrderEmptyView alloc] init];
    }
    return _emptyView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
